
const refreshNomalTitle = '下啦可以刷新';
const refreshPullTitle = '松手立即刷新';
const refreshingTitle = '正在刷新';
const footerRefresingTitle = '正在努力加载';
const footerNoMoreDataTitle = '没有更多数据了';


/**为需要的图片base64 */
const refreshBase64ArrowImage = '';


const refreshBase64RoundImage = '';


var init = function (scrollHeight) {
  let that = this;
  that.setData({
    refreshHeadData: {
      refreshHeadHeight: 0,
      refreshHeadRotate: 0,
      refreshHeadTitle: refreshNomalTitle,
      refreshHeadStatus: 0,
      endHeadRefresh: false,
      endHeadRefreshPosition: 'absolute',
      refreshHeadBase64RoundImage: refreshBase64RoundImage,
      refreshHeadBase64ArrowImage: refreshBase64ArrowImage
    },
    refreshFooterData: {
      refreshFooterStatus: 0,
      endFooterRefresh: false,
      refreshFooterTitle: footerRefresingTitle,
      refreshFooterNoMore: false,
      refreshFooterBase64RoundImage: refreshBase64RoundImage,
      refreshFooterNoMoreDataTitle: footerNoMoreDataTitle
    }
  }),
    that.scroll = (options) => {
      let that = this
      let scrollTop = options['scroll'].detail['scrollTop'];
      if (scrollTop <= 0) {///下啦刷新
        scrollTop = -scrollTop;
        that.scrollPulldownAction(scrollTop, options.pullDownAction);
      } else if (options['scroll'].detail['scrollHeight'] - scrollTop - scrollHeight < 60) {///上啦加载
        that.scrollPullOnAction(scrollTop, options.pullOnAction);
      }
    },
    /**
     * 下啦刷新的action
     */
    that.scrollPulldownAction = (scrollTop, pullDownAction) => {
      if (this.data.refreshHeadData.refreshHeadStatus == 1) {
        return;
      }
      if (scrollTop > 60) {/// 
        if (this.data.refreshHeadData.endHeadRefresh) {/// 手指松开了  开始刷新
          this.setData({
            'refreshHeadData.refreshHeadTitle': refreshingTitle,
            'refreshHeadData.refreshHeadStatus': 1,
            'refreshHeadData.endHeadRefreshPosition': 'relative',
            'refreshHeadData.refreshHeadHeight': 60,
            'refreshHeadData.refreshHeadRotate': 180,
            'refreshHeadData.refreshHeadHeight': 60,
          })
          pullDownAction();/// 回掉开始刷新的block
          return;
        }
     
        this.setData({
          'refreshHeadData.refreshHeadHeight': 60,
          'refreshHeadData.refreshHeadRotate': 180,
          'refreshHeadData.refreshHeadTitle': refreshPullTitle,
        });
      } else {
        var rotate = 3 * scrollTop;
        this.setData({
          'refreshHeadData.refreshHeadHeight': scrollTop,
          'refreshHeadData.refreshHeadRotate': rotate,
          'refreshHeadData.refreshHeadTitle': refreshNomalTitle,
          'refreshHeadData.endHeadRefresh': false,
        });
      }
    },
    /**
     * 上啦刷新的action
     */
    that.scrollPullOnAction = (scrollTop, pullOnAction) => {
      if (this.data.refreshFooterData.refreshFooterNoMore) {/// 没有数据的时候
        return;
      }
      if (this.data.refreshFooterData.refreshFooterStatus == 1) {/// 刷新状态
        return;
      }
      this.data.refreshFooterData.refreshFooterStatus = 1;
      this.setData({
        refreshFooterData: this.data.refreshFooterData
      })
      pullOnAction();
    },
    /**
     * 手指松开
     */
    that.touchEnd = () => {
      this.data.refreshHeadData.endHeadRefresh = true;
    },
    /**
     * 结束刷新
     */
    that.endScroll = (options) => {
      if (typeof options === 'undefined') {
        options.noMoreData = false;
      }
      this.setData({
        'refreshHeadData.refreshHeadStatus' : 0,
        'refreshHeadData.refreshHeadTitle': '结束刷新',
        'refreshHeadData.refreshHeadHeight': 0,
        'refreshHeadData.endHeadRefresh': false,
        'refreshHeadData.endHeadRefreshPosition': 'absolute',
        'refreshHeadData.refreshHeadRotate': 0,

        'refreshFooterData.refreshFooterStatus':0,
        'refreshFooterData.refreshFooterTitle': footerRefresingTitle,
        'refreshFooterData.refreshFooterNoMore': options.noMoreData,
        'refreshFooterData.refreshFooterStatus': 0,
      })
    }
}
module.exports = {
  init,
}